package cn.yunhe.test;

import cn.yunhe.eneity.Emp;
import cn.yunhe.service.IEmpService;
import cn.yunhe.service.impl.EmpService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Demo {
    ApplicationContext ac;

    @Before
    public void init(){
        ac=new ClassPathXmlApplicationContext("spring.xml");

    }
    @Test
    public void test1(){
        IEmpService empService= (IEmpService) ac.getBean("empService");
        Emp emp=new Emp();
        emp.setEname("S");
        List<Emp> list=empService.searchEmp(emp);
        for (Emp e:list
             ) {
            System.out.println(e.getEname());
        }
//        empService.addEmp(new Emp(11,"aasd","asd"));
    }

    @Test
    public void test2(){
        List<Emp> list=new ArrayList<>();
        //正确的，会在数据库中添加
//        for (int i = 0; i <3 ; i++) {
//            Emp emp=new Emp();
//            emp.setEmpno(9002+i);
//            emp.setEname("zhang");
//            list.add(emp);
//        }

        //错的，事物回滚
        for (int i = 0; i <3 ; i++) {
            Emp emp=new Emp();
            if (i==2){
                emp.setEmpno(20+i);
                emp.setEname(null);
            }else {
                emp.setEmpno(20+i);
                emp.setEname("lisi");

            }

        }


        IEmpService es= (IEmpService) ac.getBean("empService");
        es.addEmp(list);



    }



}
